package com.woan.api.servlet;


import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysRole;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;
import com.woan.api.services.UserClientServices;
import com.woan.api.services.VideoClientServices;
import com.woan.api.util.FuncDate;
import com.woan.api.util.HttpTool;

import net.sf.json.JSONObject;

public class VideoLiveUMu extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpTool ht = new HttpTool();
	
	protected FuncDate fd = null;

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		Hashtable<String, String> hash = null;
		hash = this.ht.getRequestBody(request);
			System.out.println("视频服务器发送的内容---------"+hash);
			String str1=null;
			if (hash==null) {
				str1=request.getParameter("type");
			}else {
				str1 = (String) hash.get("type");
			}
		
			//如果是m3u8则需要取到type=7，这里7的通知是循环发送切片的，所以只处理通知7里面sequence=0的内容就可以	
			//直播开始
			if(str1.equals("0"))
			{		
				//如果是m3u8，直播开始的时候传入状态status=2，这个时候还不能播放，等待通知7以后才可以播放，所以存入其他的status
				videoMUStart(request, response,hash);	
			}
			//m3u8可以开始播放
			if(str1.equals("7"))
			{
				//首先获取录制的视频格式
				String sequence =(String)hash.get("sequence");
				System.out.println("m3u8切片---------"+sequence);
				if(sequence.equals("0"))
				{
					videoMUReady(request, response,hash);
				}
			}
//			if (str1.equals("1"))
//			{	
//				videoStop(request, response);
//			}
				
			if (str1.equals("2"))
			{
				videoArchive(request, response,hash);
			}
			
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
	}

	public void videoMUReady(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
		String msg = "-1";
		System.out.println("----------视频M3U8直播开始-----------");
		System.out.println("----------视频开始消息内容-----------" + hash);
		String vid = (String) hash.get("hashed_id");
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
		SysVideo sysVideo = videoClientServices.listOneVideoinfo(vid, "ios");
		SysVideoBasic sysVideoBasic = videoClientServices.getBasicVideoByVid(vid);
		SysRole sysRole = userClientServices.findRoleByRid(sysVideoBasic.getRid());
		sysVideo.setStatus(0);
		videoClientServices.updateVideo(sysVideo,sysVideoBasic.getRid(),sysRole.getRoleName(),sysVideoBasic.getUserName());
		videoClientServices.pushMessageM3u8(sysVideo,sysVideoBasic.getRid(),sysRole.getRoleName(),sysVideoBasic.getUserName());
		System.out.println("M3U8直播更新视频");
	}
	public void videoMUStart(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
		System.out.println("----------视频M3U8待播放状态-----------");		
		System.out.println("----------视频开始消息内容-----------" + hash);
		String vid = (String) hash.get("hashed_id");
		String user_name = "liveu|liveu01";
		String videoPath = (String) hash.get("url");
		String videoSize = (String) hash.get("size");
		String groupname = "";
		String username = "";
		if (user_name.contains("|")) 
		{
			String para[] = user_name.split("\\|");
			groupname = para[0];
			username = para[1];
		}
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        SysAdmininfo user = null;
        user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
		String uid = "";
		if (user != null) {
			uid = user.getUid();
			//videoClientServices.updateVideoState(uid);
		} 
		SysVideo sysVideo1 = videoClientServices.listOneVideoinfo(vid, "ios");	
		if(sysVideo1==null)
		{
			SysVideo sysVideo = new SysVideo();
			sysVideo.setVid(vid);
			sysVideo.setBeginTime(FuncDate.FindCurSysTime1());
			sysVideo.setEndTime("");
			sysVideo.setPicture("");
			sysVideo.setStatus(2);
			sysVideo.setVideoLength("");
			sysVideo.setVideoPath(videoPath);
			sysVideo.setVideoSize(videoSize);
			sysVideo.setVideoType("ios");
			sysVideo.setIsdel("1");		
			System.out.println("m3u8直播插入视频");
			videoClientServices.editVideos(user.getSysRole().getRid(),user.getUid(),user.getUserName(),vid, "暂无标题","","","","","","","","","");
			videoClientServices.insertVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
		}
	}
	
	
	public void videoStop(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
//		System.out.println("---------------- 视频M3u8停止通知 -----------");
//		System.out.println("----------this.hash-----------" + this.hash);
//		String video_code = (String) this.hash.get("hashed_id");
//		String curtime = FuncDate.FindCurSysTime1();
//		VOESysVideo video = new VOESysVideo();
//		BOQVideo boqvideo = new BOQVideo();
//		video = boqvideo.FindvideoByHashid(video_code);
//		video.setVid(video_code);
//		video.setEndtime(curtime);
//		
//		video.setStatusM3u8("1");
//		BLVideo blvideo = new BLVideo();
//		String resu = "0";
//		resu = blvideo.updateVideo(video);
//		String msg = "";
//		if (resu.equals("0")) {
//			msg = "0";
//			this.ht.retData(response, msg);
//		} else {
//			msg = "-1";
//			this.ht.retData(response, msg);
//		}
	}

	public void videoArchive(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
		System.out.println("---------------- M3u8存档通知 -----------");
		System.out.println("----------M3u8存档通知 this.hash-----------" + hash);
		String vid = (String) hash.get("hashed_id");
		String videoLength = (String) hash.get("duration");	
		String videoPath = (String) hash.get("url");
		String curtime = FuncDate.FindCurSysTime1();
		String user_name = "liveu|liveu01";
		String groupname = "";
		String username = "";
		if (user_name.contains("|")) 
		{
			String para[] = user_name.split("\\|");
			groupname = para[0];
			username = para[1];
		}
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        SysAdmininfo user = null;
        user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
		SysVideo sysVideo = videoClientServices.listOneVideoinfo(vid, "ios");	
		sysVideo.setVideoPath(videoPath);
		sysVideo.setPicture(videoPath+".jpg");
		sysVideo.setStatus(1);
		sysVideo.setEndTime(FuncDate.FindCurSysTime1());
		sysVideo.setVideoLength(videoLength);
		videoClientServices.updateVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
		System.out.println("m3u8存档更新视频");
	}
}